//
//  InviteViewController.m
//  UniversalApp
//
//  Created by 志刚杨 on 2018/3/23.
//  Copyright © 2018年 voidcat. All rights reserved.
//

#import "InviteViewController.h"
#import "InvitationHeaderView.h"
#import "InvitaionTableViewCell.h"
#import "InviteLogic.h"
@interface InviteViewController ()<QMUITableViewDelegate,QMUITableViewDataSource,InviteDelegate>
@property(nonatomic, strong) QMUIFillButton *buttonCpoy;
@property(nonatomic, strong) QMUIFillButton *buttonSharUrl;
@property(nonatomic, strong) InvitationHeaderView *headerView;

@property(nonatomic, strong) InviteLogic *logic;
@end

@implementation InviteViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createUI];
    _logic = [[InviteLogic alloc] init];
    _logic.delegagte = self;
    [self loadData];
    
}

- (void)loadData {
    [_logic loadData];
}

-(void)createUI
{
    self.title = ASLocalizedString(@"邀请好友");
    _buttonCpoy = [[QMUIFillButton alloc] initWithFillColor:MainColor titleTextColor:KWhiteColor];
    [_buttonCpoy setTitle:ASLocalizedString(@"点击复制邀请码:[加载中]")forState:UIControlStateNormal];
    [_buttonCpoy addTarget:self action:@selector(handleCodeCopyEvent:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_buttonCpoy];
    
    _buttonSharUrl = [[QMUIFillButton alloc] initWithFillColor:MainColor titleTextColor:KWhiteColor];
    [_buttonSharUrl setTitle:ASLocalizedString(@"点击复制邀请链接")forState:UIControlStateNormal];
    [_buttonSharUrl addTarget:self action:@selector(handleCodeCopyEvent:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_buttonSharUrl];
    
    
    _headerView = [InvitationHeaderView getView];
    
    [self.view addSubview:_headerView];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    NSString *cellid = NSStringFromClass([InvitaionTableViewCell class]);
    
    [self.tableView registerNib:[UINib nibWithNibName:cellid bundle:nil] forCellReuseIdentifier:cellid];
    [self.view addSubview:self.tableView];
    
    //设置约束
    [self setLayout];
}

-(void)setLayout
{
    [_buttonCpoy mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@20);
        make.width.equalTo(self.view).offset(-8);
        make.height.equalTo(@40);
        make.centerX.equalTo(self.view);
    }];
    
    [_buttonSharUrl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_buttonCpoy.mas_bottom).offset(5);
        make.width.equalTo(self.view).offset(-8);
        make.height.equalTo(@40);
        make.centerX.equalTo(self.view);
    }];
    
    [_headerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(self.view);
        make.height.equalTo(@100);
        make.top.equalTo(_buttonSharUrl.mas_bottom).offset(10);
        make.left.equalTo(self.view);
    }];
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(self.view);
        make.top.equalTo(_headerView.mas_bottom).offset(5);
        make.bottom.equalTo(self.view.mas_bottom).offset(5);
    }];
    
}


#pragma mark ————— 下拉刷新 —————
-(void)headerRereshing{
    _logic.page = 1;
    [_logic loadData];
    
}

#pragma mark ————— 上拉刷新 —————
-(void)footerRereshing{
    _logic.page+=1;
    [_logic loadData];
}

#pragma mark ————— 数据拉取完成 渲染页面 —————
-(void)requestDataCompleted{
    [_buttonCpoy setTitle:[NSString stringWithFormat:ASLocalizedString(@"点击复制邀请码:%@"),_logic.invite_code] forState:UIControlStateNormal];
    
    _headerView.z_income.text = _logic.income_total;
    _headerView.income.text = _logic.income_total;
    _headerView.user_count.text = _logic.invite_user_count;
    
    [self.tableView.mj_footer endRefreshing];
    [self.tableView.mj_header endRefreshing];
    
    [UIView performWithoutAnimation:^{
        [self.tableView reloadData];
    }];
    
}

//点击复制邀请码
- (void)handleCodeCopyEvent:(QMUIFillButton *)button {
    if(button == _buttonCpoy)
    {
        if(StrValid(_logic.invite_code))
        {
            [UIPasteboard generalPasteboard].string = _logic.invite_code;
            [HUDHelper alert:NSStringFormat(ASLocalizedString(@"邀请码 %@ 已复制到粘贴板"),_logic.invite_code)];
        }
    }
    else
    {
        [UIPasteboard generalPasteboard].string = [NSString stringWithFormat:@"%@",KGlobalVariable.appmodel.invite_reg_url];
        [HUDHelper alert:NSStringFormat(ASLocalizedString(@"已复制"))];
    }
    
}

#pragma mark -tableview

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _logic.dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 54;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    InvitaionTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([InvitaionTableViewCell class])];
    InvitationModel *model = _logic.dataArray[indexPath.row];
    [cell setModel:model];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
